//@author A0106044W
package logic;

import commandInfo.CommandInfo;
import commandInfo.StatusCommandInfo;
import storage.Task;

/**
 * 
 * 
 * 	       Sothearith Sreang
 *         All of the Logic part is written by me.
 * 
 */

public class PendingHandler extends OperationHandler {

	Task task;
	boolean status;
	
	private static final String SUCCESSFUL_PENDING_MESSAGE = "The task with description \"%s\" is successfully set to pending";
	private static final String SUCCESSFUL_UNDO_MESSAGE = "The task with description \"%s\" is successfully set to its previous status";
	
	
	private static final String COMMAND_COMPLETE = "COMPLETE";
	private static final String COMMAND_PENDING = "PENDING";
	
	// Two public constructors
	
	//for normal setPending operation
	public PendingHandler (Task taskAtt){
		super();
		task = taskAtt;
	}
		
	//for undo setPending operation
	public PendingHandler(CommandInfo commandInfoP){
		super();
		setTask(commandInfoP);
	}
	

	// This method is called for user command's pending operation
	public String execute(){
		status = task.getDoneStatus();
		task.setPending();
		super.commandInfo = new StatusCommandInfo(COMMAND_PENDING, task, status); 
		String feedback = String.format(SUCCESSFUL_PENDING_MESSAGE, task.getDescription());
		
		return feedback;
	}
	
	// This method is called for undo for previous pending operation
	public String undo(){
		task.setStatus(status);
		super.commandInfo = new StatusCommandInfo(COMMAND_COMPLETE, task, false);
		String feedback = String.format(SUCCESSFUL_UNDO_MESSAGE, task.getDescription());
		
		return feedback;
	}
	
	
	// next level abstraction
	
	void setTask(CommandInfo commandInfoP){
		Task taskClone = commandInfoP.getUpdatedTask();
		task = super.getTask(taskClone);
		assert task != null;
		status = commandInfoP.getStatus();
	}
}
